home *** CD-ROM | disk | FTP | other *** search
- Path: news.lainet.com!usenet
- From: jason@lainet.com (Jason L. Pollack)
- Newsgroups: comp.lang.c++
- Subject: Crashing bug in constructor w/new
- Date: 26 Jan 1996 06:17:52 GMT
- Organization: LA Internet (310)442-4670
- Message-ID: <4e9rmg$af0@lainet2.lainet.com>
- NNTP-Posting-Host: a2p10r.lainet.com
- Mime-Version: 1.0
-
- I'm having a hard time with this problem. There is a crash when I try
- to new within a constructor. I'm not sure if this is a bug in the
- compiler, a restriction of the language, or a bug in the programmer's
- wetware.
-
- I have a class that contains objects of another class whose constructor
- puts it on a linked list, allocating a new node on the list with new.
-
- It looks something like this:
-
- class ClassX {
- ClassY array[15];
- //...and other stuff, of course.
- };
-
- class ClassY {
- ClassY() {
- YController::Add(this);
- };
- ~ClassY();
- //...and other stuff...
- };
-
- class YController {
- static node * list;
- static void Add(ClassY* item)
- {
- //bounds checking and normal linked list stuff here...
- node * n = new node(item,NULL);
- list = n;
- }
- static class node {
- node(ClassY * it, node* nx) { item = it; nx = next; }
- ClassY * item;
- node * next;
- };
- };
- YController::node * YController::list = NULL;
-
-
- This is the bare shell of my program. There is a lot of code missing
- that handles the linked list, but I want to draw your attention to the
- line containing new in YController::Add().
-
- In my program, I do this:
-
- ClassX xx = new ClassX;
-
- And the program crashes. I've traced it down to the constructor of
- the 15th element in array[]. The linked list of ClassY items is
- correct until it constructs array[14]. What's happening is that new
- is allocating nodes where it should have already allocated memory
- for ClassX! As I trace the program, I see that it is correctly
- maintaining the list as I expect, but when it allocates array[14], it
- puts the object somewhere in the list and corrupts memory.
-
- Clearly it had no choice where to put array[14], it's part of the space
- of ClassX. I want to know why it put the beginning of my linked list
- (the first node, or any node for that matter) in a part of memory it
- already knew xx must reside.
-
- I'm using Microsoft's VC++1.5 on a Win95 system.
-
- Does anyone know what is causing this problem? I've used new to
- allocate memory in plenty of other constructors before; I'm surprised
- at this behavior. Is it a compiler bug, or is there something more
- subtle that I'm missing?
-
- Thanks for your help,
- Jay
-
-
-